package ui.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.AddCardActivity;
import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Bean.GetBankcardBean;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.ListViewFinal;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import coom.Constants;
import event.BankCardEvent;
import event.DeleteCardEvent;
import event.NetErrorEvent;
import ui.activity.mine.biz.BankCardBiz;
import ui.activity.mine.component.DaggerBankCardComponent;
import ui.activity.mine.contract.BankCardContract;
import ui.activity.mine.module.BankCardModule;
import ui.activity.mine.presenter.BankCardPresenter;
import ui.adapter.BankCardAdapter;
import util.AlertUtils;
import util.CommonViewUtil;
import util.StringUtils;
import widget.alertview.AlertView;


/**
 * 银行卡列表
 * Created by chenjian on 2017/8/4.
 */

public class BankCardAct extends BaseAct implements BankCardContract.View {


    @Inject
    BankCardPresenter presenter;
    @Inject
    BankCardBiz biz;

    @BindView(R.id.ptr_list_view)
    ListViewFinal rv;
    @BindView(R.id.needsx)
    TextView needsx;

    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    LinearLayout add;

    private SharedPreferences sp;

    BankCardAdapter adp;
    private static final int REQUEST_CODE_ADD_CARD = 101;
    String type = "0";

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_bank_card);
    }

    @Override
    protected void setup() {
        DaggerBankCardComponent.builder().bankCardModule(new BankCardModule(this)).build().inject(this);
        CommonViewUtil.setDarw(needsx, this,-1 , -1,R.mipmap.setting,-1);
        setToolBar(getResources().getString(R.string.bankcark), "");
        presenter.setBiz(biz);

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        type = StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING)) ? "0" : getIntent().getStringExtra(Constants.KEYSTRING);
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        View footer = View.inflate(this, R.layout.footer_bank_card, null);
        add = footer.findViewById(R.id.layout_add);
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(() -> {
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getCardList(true);
            }
        });
        adp = new BankCardAdapter(this);
        rv.addFooterView(footer);
        rv.setAdapter(adp);
        rv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!type.equals("0")) {
                    EventBus.getDefault().post(new BankCardEvent(adp.getData().get(position)));
                    finish();
                }
            }
        });

        RxView.clicks(add)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    Intent intent = new Intent(BankCardAct.this, AddCardActivity.class);
                    startActivityForResult(intent, REQUEST_CODE_ADD_CARD);
                });


        RxView.clicks(needsx)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                   presenter.goToChangeBank();
                });
//        ptr.autoRefresh(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getCardList(true);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void upDateUI(List<GetBankcardBean.DataBean.ItemsBean> bean) {
        adp.getData().clear();
        adp.setData(bean);
        adp.notifyDataSetChanged();
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }

    /**
     * 删除卡
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerDeleteCard(DeleteCardEvent event) {
        AlertView checkAlert = AlertUtils.showInfo(BankCardAct.this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    if (event.getDefaultCard() == 1) {
                        showMsg("暂不支持删除默认银行卡");
                    } else {
                        presenter.deleteCard(event.getCardId(), event.getPosition());
                    }

                }
            }
        }, "提示", "是否删除此银行卡？");
        checkAlert.show();
    }

    /**
     * 新增卡片刷新
     *
     * @param event
     */
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void AddCard(AddCardEvent event) {
//        presenter.getCardList(true);
//    }
}
